﻿using System.Windows;
using System.Reflection;
using System;
using HBCode.Presentation;
using AvalonDockMVVM;
using System.ComponentModel;

namespace HBCode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Convenient accessor for the main view-model.
        /// </summary>
        private MainViewModel ViewModel
        {
            get
            {
                return (MainViewModel)this.DataContext;
            }
        }

        /// <summary>
        /// Name of the file used to save/restore AvalonDock layout.
        /// </summary>
        private static readonly string LayoutFileName = "MyLayoutFile.xml";

        /// <summary>
        /// Name of the embedded resource that contains the default AvalonDock layout.
        /// </summary>
        private static readonly string DefaultLayoutResourceName = "HBCode.Resources.DefaultLayoutFile.xml";

        /// <summary>
        /// Event raised when AvalonDock has loaded.
        /// </summary>
        private void avalonDockHost_AvalonDockLoaded(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(LayoutFileName))
            {
                //
                // If there is already a saved layout file, restore AvalonDock layout from it.
                //
                avalonDockHost.DockingManager.RestoreLayout(LayoutFileName);
            }
            else
            {
                //
                // This line of code can be uncommented to get a list of resources.
                //
                //string[] names = this.GetType().Assembly.GetManifestResourceNames();

                //
                // Load the default AvalonDock layout from an embedded resource.
                //
                var assembly = Assembly.GetExecutingAssembly();
                using (var stream = assembly.GetManifestResourceStream(DefaultLayoutResourceName))
                {
                    if (stream != null)
                        avalonDockHost.DockingManager.RestoreLayout(stream);
                }
            }
        }

        /// <summary>
        /// Event raised when the window is about to close.
        /// </summary>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            //
            // Notify the view-model that the application is closing,
            // allows the view-model the chance to cancel application exit.
            //
            if (!this.ViewModel.OnApplicationClosing())
            {
                //
                // The view-model has cancelled application exit.
                // This will happen when the 1 or more documents have been modified but not saved
                // and the user has selected 'No' when asked to confirm application exit.
                //
                e.Cancel = true;
                return;
            }

            //
            // When the window is closing, save AvalonDock layout to a file.
            //
            avalonDockHost.DockingManager.SaveLayout(LayoutFileName);
        }

        private void avalonDockHost_DocumentClosing(object sender, DocumentClosingEventArgs e)
        {

        }
    }
}
